﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script runat="server" type="text/C#">
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void PagerCommand(object sender, DataPagerCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Next":
                    //  guard against going off the end of the list
                    e.NewStartRowIndex = Math.Min(e.Item.Pager.StartRowIndex + e.Item.Pager.MaximumRows, e.Item.Pager.TotalRowCount - e.Item.Pager.MaximumRows);
                    e.NewMaximumRows = e.Item.Pager.MaximumRows;
                    break;
                case "Previous":
                    //  guard against going off the begining of the list
                    e.NewStartRowIndex = Math.Max(0, e.Item.Pager.StartRowIndex - e.Item.Pager.MaximumRows);
                    e.NewMaximumRows = e.Item.Pager.MaximumRows;
                    break;
                case "Last":
                    //  the
                    e.NewStartRowIndex = e.Item.Pager.TotalRowCount - e.Item.Pager.MaximumRows;
                    e.NewMaximumRows = e.Item.Pager.MaximumRows;
                    break;
                case "First":
                default:
                    e.NewStartRowIndex = 0;
                    e.NewMaximumRows = e.Item.Pager.MaximumRows;
                    break;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void LvSorting(object sender, ListViewSortEventArgs args)
        {
            this.lds.OrderGroupsBy = string.Format("Key {0}", args.SortDirection);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void LvSorted(object sender, EventArgs args)
        {
            ImageButton btn = this.lv.FindControl("btnSort") as ImageButton;
            btn.ImageUrl = string.Format("~/_assets/img/{0}", this.lv.SortDirection == SortDirection.Ascending ? "asc.png" : "desc.png");
        }

        protected void lds_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {

        }
</script>
    <style type="text/css">
        .grid
        {
         	border:solid 2px #a5a4bd;        	
        	font-family:Tahoma;
        	font-size:12px;
        	width:600px;
        }
        .grid H2
         {
         	background:#8988a5 url(_assets/img/title-bg.gif);
         	border-bottom:solid 1px #57566f;
         	height:28px;
         	line-height:28px;
         	margin:0px;
         	padding:0px 0px 0px 5px;
        	color:#fff;
        	font-size:15px;
         }
         .grid TABLE
         {
         	width:600px;
         }
         
         /* grid headers */
         .grid #orders .head TH
         {
         	background:#e9e9eb url(_assets/img/header-bg.gif);
         	height:24px;
         	line-height:24px;
         	padding-left:6px;
         	color:#666666;
         	text-align:left;
         }
         .grid #orders .head TH.first
         {
         	/* get rid of the first slash */
         	background:#e9e9eb url(_assets/img/header-bg.gif) -3px;
         	width:20px;
         } 
         
         /* group header */
         .grid #orders .group TH
         {
         	padding:10px 0px 2px 0px;
         	color:#666666;
         	text-align:left;
         	border-bottom:solid 2px #a5a4bd;
         } 
         .grid #orders .group TH.first
         {
         	text-align:center;         	
         	border-bottom:none;
         }              
         
         /* data items */                                       
         .grid #orders .item TD
         {
         	padding:3px 0px 3px 6px;
         	border-bottom:solid 1px #eae9e1; 
         	color:#222222;       	
         } 
         
         /* pager */    
        .grid #pager .pager TD
        {
        	height:40px;
        }  
        .grid #pager .pager .commands
        {
        	height:40px;
            padding:4px 0px 0px 25px;
        }               
        .grid #pager .pager .info
        {
            padding:4px 25px 0px 0px;
        	text-align:right;
        }       
        .hidden
        {
        	display:none;
        	visibility:hidden;
        }      
    </style>
    <script type="text/javascript">
        function toggleGroup(img, numberOfRows){
            //  get a reference to the row and table
            var tr = img.parentNode.parentNode;
            var table = $get('orders');
            var src = img.src;
            
            //  do some simple math to determine how many
            //  rows we need to hide/show
            var startIndex = tr.rowIndex + 1;
            var stopIndex = startIndex + parseInt(numberOfRows);
            
            //  if the img src ends with plus, then we are expanding the
            //  rows.  go ahead and remove the hidden class from the rows
            //  and update the image src
            if(src.endsWith('plus.png')){
                for(var i = startIndex; i < stopIndex; i++){
                    Sys.UI.DomElement.removeCssClass(table.rows[i], 'hidden');
                }
            
                src = src.replace('plus.png', 'minus.png');
            }
            else{
                for(var i = startIndex; i < stopIndex; i++){
                    Sys.UI.DomElement.addCssClass(table.rows[i], 'hidden');
                }
                            
                src = src.replace('minus.png', 'plus.png');
            }
            
            //  update the src with the new value
            img.src = src;
        }
    </script>
</head>
<body>
    <form id="frm" runat="server">
        <div>
            <asp:ScriptManager ID="scriptManager" runat="server" />
            <asp:LinqDataSource 
                ID="lds" runat="server"
                ContextTypeName="PlacementManagerLinqDataContext"
                TableName="PlacementTypes"  
                Select="new(Key, Count()  as Count, It as Items)" 
                GroupBy="new(DateStart, DateEnd, PlacementTypeName, Year, SemesterId, UnitId, CategoryId, AttendancePatternId,RequiredHours)" onselecting="lds_Selecting"
            />            
            <asp:UpdatePanel ID="upd" runat="server">
                <ContentTemplate>
                    <asp:ListView ID="lv" runat="server" DataSourceID="lds" OnSorting="LvSorting" OnSorted="LvSorted">
                        <LayoutTemplate>
                            <div class="grid">
                                <h2>Current Placements </h2>
                                <table id="orders" cellpadding="0" cellspacing="0">
                                    <tr class="head">
                                        <th class="first"></th>
                                        <th>Start</th>
                                        <th>End</th>
                                        <th>Name</th>
                                        <th>Year</th>
                                        <th>Sem</th>
                                        <th>Unit</th>
                                        <th>Campus</th>
                                        <th>Category</th>
                                        <th>Attten Pattern</th>
                                        <th>Req Hours</th>
                                        <th>Allocations</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server" />                         
                                </table>
                                <table id="pager" cellpadding="0" cellspacing="0">
                                    <tr class="pager">
                                        <asp:DataPager ID="pager" runat="server" PageSize="8">
                                            <Fields>
                                                <asp:TemplatePagerField OnPagerCommand="PagerCommand">
                                                    <PagerTemplate>
                                                        <td class="commands">
                                                            <asp:ImageButton ID="btnFirst" runat="server" CommandName="First" ImageUrl="~/_assets/img/first.gif" AlternateText="First Page" ToolTip="First Page" />
                                                            <asp:ImageButton ID="btnPrevious" runat="server" CommandName="Previous" ImageUrl="~/_assets/img/prev.gif" AlternateText="Previous Page" ToolTip="Previous Page" />    
                                                            <asp:ImageButton ID="btnNext" runat="server" CommandName="Next" ImageUrl="~/_assets/img/next.gif" AlternateText="Next Page" ToolTip="Next Page" />
                                                            <asp:ImageButton ID="btnLast" runat="server" CommandName="Last" ImageUrl="~/_assets/img/last.gif" AlternateText="Last Page" ToolTip="Last Page" />                                                                                                           
                                                        </td>
                                                        <td class="info">
                                                            Page 
                                                            <b>
                                                                <%# Container.TotalRowCount > 0 ? Math.Ceiling(((double)(Container.StartRowIndex + Container.MaximumRows) / Container.MaximumRows)) : 0 %>
                                                            </b>
                                                            of
                                                            <b>
                                                                <%# Math.Ceiling((double)Container.TotalRowCount / Container.MaximumRows)%>
                                                            </b>
                                                            (<%# Container.TotalRowCount %> items)  
                                                        </td>                                                          
                                                    </PagerTemplate>
                                                </asp:TemplatePagerField>
                                            </Fields>
                                        </asp:DataPager>
                                    </tr>                                 
                                </table>
                            </div>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <tr id="row" runat="server" class="group">
                                <th class="first">
                                    <img src="../App_Themes/Default/Images/plus.png"  alt='Group: <%# Eval("Key")%>' onclick="toggleGroup(this, '<%# Eval("count") %>');" />
                                </th>
                                <th colspan="5"><%# Eval("Key.DateStart", "{0:MM/dd/yyyy}")%>  <%# Eval("Key.DateEnd", "{0:MM/dd/yyyy}")%>  <%# Eval("Key.PlacementTypeName")%> <%# Eval("Key.Year")%> <%# Eval("Key.PlacementTypeName")%></th>
                            </tr>
                            <asp:ListView ID="lvItems" runat="server" DataSource='<%# Eval("Items") %>'>
                                <LayoutTemplate>
                                    <tr runat="server" id="itemPlaceholder" />
                                </LayoutTemplate>
                                <ItemTemplate>
                                    <tr id="row" runat="server" class="item hidden">
                                        <td class="first"></td>
                                        <td><%# Eval("DateStart", "{0:MM/dd/yyyy}") %></td>
                                        <td><%# Eval("DateEnd", "{0:MM/dd/yyyy}")%></td>
                                        <td><%# Eval("Campus.CampusName")%></td>
                                        <td><%# Eval("Year") %></td>
                                        <td><%# Eval("Users.Username")%></td>
                                        <td><%# Eval("UnitID")%></td>
                                    </tr>
                                </ItemTemplate>
                            </asp:ListView>
                        </ItemTemplate>
                    </asp:ListView>
                </ContentTemplate>
            </asp:UpdatePanel>            
        </div>
    </form>
</body>
</html>
